//
//  LLGStatuseRetweetView.swift
//  Weibo30LLG
//
//  Created by 李龙 on 16/8/12.
//  Copyright © 2016年 lilong. All rights reserved.
//

import UIKit
import SnapKit

class LLGStatuseRetweetView: UIView {
    
    //记录retweetView的底部
    var retweetViewBottomConstraint: Constraint?
    
    var statuseViewModel: LLGStatuseViewModel? {
    
        didSet {
        
            contentLabel.text = statuseViewModel?.retweetContent
            
            //取消约束
            retweetViewBottomConstraint?.uninstall()
            
            //配图信息存在且不为0
            if let picUrls = statuseViewModel?.statuse?.retweeted_status?.pic_urls where picUrls.count > 0 {
            
                //显示
                pictureView.hidden = false
                
                pictureView.picUrls = picUrls
                
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(StatuseTableViewCellMargin).constraint
                })
                
            } else {
            
                //隐藏
                pictureView.hidden = true
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.retweetViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(StatuseTableViewCellMargin).constraint
                })
                
            }
        }
    }
    
    //配图控件
    private lazy var pictureView: LLGPictureView = {
    
        let view = LLGPictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()

    //转发内容微博显示数据
    private lazy var contentLabel: UILabel = {
        
        let label = UILabel(fontSize: 14, color: UIColor.darkGrayColor())
        label.numberOfLines = 0
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: --添加控件,设置约束
    private func setupUI() {
    
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        //添加子控件
        addSubview(contentLabel)
        addSubview(pictureView)
        
//        contentLabel.text = "好好学习,天天向上,好好学习,天天向上,好好学习,天天向上,好好学习,天天向上,好好学习,天天向上"
        
        //设置约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(self).offset(StatuseTableViewCellMargin)
            make.top.trailing.equalTo(self).offset(StatuseTableViewCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(StatuseTableViewCellMargin)
        }
        
        self.snp_makeConstraints { (make) -> Void in
             self.retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(StatuseTableViewCellMargin).constraint
        }
        
    }

}
